package com.rd.student.jike.class4.xiayinyin.week04.day25;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class TestExecutor1 {

    private static final int CORE_POOL_SIZE = 5;//核心线程的数量
    private static final int MAX_POOL_SIZE = 20;//线程池的最大线程数
    private static final int QUEUE_CAPACITY = 2;//等待队列
    private static final Long KEEP_ALIVE_TIME = 1L;//非核心线程存活的最长时间

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.DiscardPolicy());
        List<Future<String>> futureList = new ArrayList<>();
        MyRunnable callable = new MyRunnable();
        for (int i = 0; i < 10; i++) {
            Future<String> future = (Future<String>) executor.submit(callable);
            futureList.add(future);
        }
        for (Future<String> future : futureList){
            try {
                future.get();
            }catch (InterruptedException e){
                throw new RuntimeException(e);
            }catch (ExecutionException e){
                throw new RuntimeException(e);
            }
//or
//            try {
//                String result = future.get();
//                System.out.println(result);
//            }catch (InterruptedException | ExecutionException e){
//                e.printStackTrace();
//            }
        }
    }
}
